-- NewSpeedDungeonsWinView 竞速副本胜利UI

local IconRender = require("app.widget.renders.IconRender")

local fileName = string.getModeName(...)
local SecretResultWinView = class(fileName, app.mvc.ViewBase)

local Res = {
    SecretWinScene = "ui2/upgrade/mj_win.csb",
    teamUIPath = "ui_resources/team/", -- 队伍UI导出资源
}

SecretResultWinView.VIEW_ROOT_RES = Res.SecretWinScene

function SecretResultWinView:init()
	self._ui.Button_close:addClickEventListener(function()
			executeFunc(self._onCloseView, self)
		end)

	-- self._ui["Panel/Panel_speed_1"]:getChildByName( "Button" ):addClickEventListener(function()
	-- 		executeFunc(self._onCloseView, self)
	-- 	end)
    
    self.rewards = {
        {
            panel = self._ui["Panel/icon_1"],
            icon = self._ui["Panel/icon_1/icon"],
            name = self._ui["Panel/icon_1/Panel_num/num"],
        },

        {
            panel = self._ui["Panel/icon_2"],
            icon = self._ui["Panel/icon_2/icon"],
            name = self._ui["Panel/icon_2/Panel_num/num"],
        },

        {
            panel = self._ui["Panel/icon_3"],
            icon = self._ui["Panel/icon_3/icon"],
            name = self._ui["Panel/icon_3/Panel_num/num"],
        },        
        }

    -- self.rewards[1].name:setString(L("铜杯奖励"))
    -- self.rewards[2].name:setString(L("银杯奖励"))
    -- self.rewards[3].name:setString(L("金杯奖励"))

    -- for i = 1, 3 do 
        -- local cs = self.rewards[i].icon:findChild("Panel"):getChildren()
        -- for _, v in ipairs(cs) do 
        --     v:hide()
        -- end
        -- self.rewards[i].panel = self.rewards[i].icon
        -- self.rewards[i].number = self.rewards[i].icon:findChild("Panel/level")
        -- self.rewards[i].icon = self.rewards[i].icon:findChild("Panel/icon")        
        -- self.rewards[i].icon:show()
        -- self.rewards[i].number:show()
        
    -- end

    -- self.rewards[1].icon:loadTexture(Res.teamUIPath .. "secret_trophy_"..tostring(3)..".png", 1)
    -- self.rewards[2].icon:loadTexture(Res.teamUIPath .. "secret_trophy_"..tostring(2)..".png", 1)
    -- self.rewards[3].icon:loadTexture(Res.teamUIPath .. "secret_trophy_"..tostring(1)..".png", 1)
end

function SecretResultWinView:refresh(data)
    local gradeStr = ""
    local grade = 0
    local curGrade = 0
    local imgIndex = protobuf.enum_id("pb_secretDungeon.REWARD", data.showGrade)
     if imgIndex == 1 then
        imgIndex = 3
    elseif imgIndex == 2 then
        imgIndex = 2
    elseif imgIndex == 3 then
        imgIndex = 1
    end

    
    local totalExp = 0
    local percent = 0
    local exps = SecretManager:getCurrentExpRewards()
    for i, v in ipairs(self.rewards) do
        self.rewards[i].panel:hide()
        -- self.rewards[i].name:setString(tostring(0))
    end
    local nodes = {}
    for i, v in ipairs(data.newRewardList) do 
        grade = protobuf.enum_id("pb_secretDungeon.REWARD", v)
        -- self.rewards[grade].mask:hide()
        self.rewards[grade].name:setString(exps[grade])
        self.rewards[grade].panel:show()
        table.insert(nodes, self.rewards[grade].panel)
        totalExp = totalExp + exps[grade]
        if grade > curGrade then
            curGrade = grade
            if curGrade == 1 then
                gradeStr = string.format("(%s)", L("copper_cup"))
                --imgIndex = 3
                percent = 33
            elseif curGrade == 2 then
                gradeStr = string.format("(%s)", L("silver_cup"))
                --imgIndex = 2
                percent = 66
            elseif curGrade == 3 then
                gradeStr = string.format("(%s)", L("gold_cup"))
                --imgIndex = 1
                percent = 100
            end
        end
    end
    if #data.newRewardList == 0 and checknumber(data.addExp) > 0 then
        self.rewards[1].name:setString(checknumber(data.addExp))
        totalExp = data.addExp

        -- self._ui["Panel/Panel_secret/Panel_top"]:hide()
    end
    
    local strTlt = L("lua_code_text_302")..SecretManager:second2msStr(data.finishSeconds)..gradeStr
    self._ui["Panel/tips_1"]:setString(strTlt)

    -- local s1 = self._ui["Panel/Panel_secret/Panel_top/bar/Slider_1"]
    -- local s2 = self._ui["Panel/Panel_secret/Panel_top/bar/Slider_2"]

    -- s1:setPercent(percent)
    -- s2:setPercent(0)

    -- if percent > 0 then
    --     -- s2:runAction(cc.ProgressTo:create(10, percent))
    --     local s2pstart = 0
    --     s2:schedule(function()
    --         s2:setPercent(math.min(s2pstart, percent))
    --         s2pstart = s2pstart + 6
    --         end
    --         , 0)
    -- else 
    --     s1:hide()
    --     s2:hide()
    -- end

    local function centerNodes(p, nodes, gap)
		if p == nil then return end

		local panelWidth = p:getContentSize().width
		local totalWidth = 0
		if nodes and #nodes > 0 then
			for i, v in ipairs(nodes) do
				totalWidth = totalWidth + v:getContentSize().width + gap
			end

			local offx = (panelWidth - totalWidth + gap) * 0.5

			for i, v in ipairs(nodes) do
				v:setPositionX(offx + v:getAnchorPoint().x * v:getContentSize().width)
				offx = offx + v:getContentSize().width + gap
			end
		end
	end
    centerNodes(self._ui["Panel"], nodes, 10)
    
    -- display.autoAdjustNodesPosX( self._ui["Panel"]:getContentSize().width, nodes, #nodes )

    for i, v in ipairs(nodes) do
        v:setPosition(cc.p(v:getPositionX() - 0, v:getPositionY()))
    end

    if imgIndex > 0 then
        self._ui["Panel/icon"]:loadTexture(Res.teamUIPath .. "team_trophy_icon_"..tostring(imgIndex)..".png", 1)
    end    
end
return SecretResultWinView
